package com.controller.dictionary;


import com.common.ResultUtil;
import com.common.WebContextHelper;
import com.common.stereotype.Module;
import com.common.stereotype.Token;
import com.common.stereotype.Uri;
import com.controller.base.BaseLocalController;
import com.entity.Manager;
import com.entity.Notice;
import com.facade.notice.INoticeFacadeCUD;
import com.facade.notice.INoticeFacadeR;
import com.query.NoticeQuery;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/notice")
@Module(parent = "商城内容模块", name = "新闻公告管理", parentModuleOrder = 9, childModuleOrder = 1)
public class NoticeController extends BaseLocalController {

    @Resource
    private INoticeFacadeR noticeFacadeR;
    @Resource
    private INoticeFacadeCUD noticeFacadeCUD;


    @ModelAttribute
    public void prepare(Long id, ModelMap modelMap) {
        HttpServletRequest request = WebContextHelper.getRequest();
        String requestUri = request.getRequestURI();
        if (requestUri.contains("modify") && null != id) {
            modelMap.addAttribute(noticeFacadeR.findByPrimaryKey(id));
        }
    }

    @RequestMapping(value = "/query.htm")
    @Uri(name = "查询", uri = "/notice/query", order = 0)
    public String query(@ModelAttribute("query") NoticeQuery query, ModelMap modelMap) {
        modelMap.addAttribute("paging", noticeFacadeR.queryPage(query));
        return "ctm/notice-List";
    }

    @RequestMapping(value = "/edit.htm")
    public String edit(Long id, ModelMap modelMap) {
        if (null != id) {
            Notice notice = noticeFacadeR.findByPrimaryKey(id);
            modelMap.addAttribute("notice", notice);
        }
        return "ctm/notice-edit";
    }

    @Token
    @ResponseBody
    @RequestMapping(value = "/add.htm", method = RequestMethod.POST)
    @Uri(name = "新增", uri = "/notice/add", order = 1)
    public ResultUtil add(@Valid Notice notice, BindingResult error) {
        ResultUtil resultUtil = validate(error);
        if (ResultUtil.SUCCESS != resultUtil.getStatus()) {
            return resultUtil;
        }
        Manager manager = WebContextHelper.getOnLineUser();
        Long managerId = manager.getId();

        notice.setCreator(managerId);

        return noticeFacadeCUD.addNotice(notice, managerId);
    }

    @Token
    @ResponseBody
    @RequestMapping(value = "/modify.htm", method = RequestMethod.POST)
    @Uri(name = "修改", uri = "/notice/modify", order = 2)
    public ResultUtil modify(@Valid Notice notice, BindingResult error) {
        ResultUtil resultUtil = validate(error);
        if (ResultUtil.SUCCESS != resultUtil.getStatus()) {
            return resultUtil;
        }
        Manager manager = WebContextHelper.getOnLineUser();
        Long managerId = manager.getId();

        notice.setCreator(managerId);

        return noticeFacadeCUD.modifyNotice(notice, managerId);
    }

    @ResponseBody
    @RequestMapping(value = "/delete.htm/{id}")
    @Uri(name = "删除", uri = "/notice/delete", order = 3)
    public ResultUtil delete(@PathVariable("id") Long id) {
        Manager manager = WebContextHelper.getOnLineUser();
        Long managerId = manager.getId();

        return noticeFacadeCUD.deleteNotice(id, managerId);
    }
}